package com.zhongshengnetwork.rightbe.lang.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.common.AppThemeUtils;
import com.zhongshengnetwork.rightbe.common.BaseAnalyticsFragment;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.login.LoginActivity;
import com.zhongshengnetwork.rightbe.wzt.view.WZTFindFragment;

import java.util.ArrayList;
import java.util.List;

public class FindFragment extends BaseAnalyticsFragment {

    private View rootView;
    private ViewPager mPaper;
    private ViewPagerIndicator viewPagerIndicator;
    private LinearLayout find_layout;
    private View home_layout_line;
    private List<Fragment> mFragments = new ArrayList<Fragment>();
    private List<String>titleList = new ArrayList<String>();

    private WZTFindFragment wztFindFragment;
    private DiscoverLangFragment discoverLangFragment;
    private OriginalFragment originalFragment;
    private SquareLangFragment squareLangFragment;

    public void scrollToFirstData(){
        if (mFragments != null && mFragments.size() > 0){
            if (mPaper.getCurrentItem() == 0){
                if (wztFindFragment != null){
                    wztFindFragment.getFirstData(true);
                }
            }else if (mPaper.getCurrentItem() == 1){

                if (squareLangFragment != null){
                    squareLangFragment.getFirstData(true);
                }

            }else if (mPaper.getCurrentItem() == 2){
                if (originalFragment != null){
                    originalFragment.getFirstData(true);
                }
            }else if (mPaper.getCurrentItem() == 3){

                if (discoverLangFragment != null){
                    discoverLangFragment.getFirstData(true);
                }
            }
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //System.out.print("首页界面创建\n");
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        if (rootView == null){
            rootView = inflater.inflate(R.layout.find_layout, container,false);
            viewPagerIndicator = (ViewPagerIndicator) rootView.findViewById(R.id.indicator);
            mPaper = (ViewPager)rootView.findViewById(R.id.viewpager);
            home_layout_line = (View) rootView.findViewById(R.id.home_layout_line);
            home_layout_line.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());

            find_layout = (LinearLayout) rootView.findViewById(R.id.find_layout);
            find_layout.setBackgroundColor(AppThemeUtils.getInstance().getMainColor());

            wztFindFragment = new WZTFindFragment();
            mFragments.add(wztFindFragment);
            titleList.add("微纸条");

            squareLangFragment = new SquareLangFragment();
            mFragments.add(squareLangFragment);
            titleList.add("广场");

            originalFragment = new OriginalFragment();
            mFragments.add(originalFragment);
            titleList.add("原创");

            discoverLangFragment = new DiscoverLangFragment();
            mFragments.add(discoverLangFragment);
            titleList.add("互动");

            discoverLangFragment = new DiscoverLangFragment();
            mFragments.add(discoverLangFragment);
            titleList.add("互动");

            discoverLangFragment = new DiscoverLangFragment();
            mFragments.add(discoverLangFragment);
            titleList.add("互动");

            discoverLangFragment = new DiscoverLangFragment();
            mFragments.add(discoverLangFragment);
            titleList.add("互动");

            discoverLangFragment = new DiscoverLangFragment();
            mFragments.add(discoverLangFragment);
            titleList.add("互动");

            discoverLangFragment = new DiscoverLangFragment();
            mFragments.add(discoverLangFragment);
            titleList.add("互动");


            FragmentStatePagerAdapter adapter = new FragmentStatePagerAdapter(getActivity().getSupportFragmentManager()) {
                @Override
                public Fragment getItem(int position) {
                    return mFragments.get(position);
                }

                @Override
                public int getCount() {
                    return mFragments.size();
                }

                @Override
                public CharSequence getPageTitle(int position) {
                    return titleList.get(position);
                }

                /**
                 * 使用这个方式，让页面不缓存，能够在清除fragment的时候对其做了删除
                 *
                 * @param object
                 * @return
                 */
                @Override
                public int getItemPosition(Object object) {
                    return PagerAdapter.POSITION_NONE;
                }

            };
            mPaper.setAdapter(adapter);
            viewPagerIndicator.setViewPager(mPaper);
            //mPaper.setCurrentItem(1);


        }else {
            //缓存的rootView需要判断是否已经被加过parent， 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
        }
        return rootView;
    }

}
